package com.ibm.spatiotemp.server.datasources.fake;

import java.util.Collection;
import java.util.Iterator;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.server.datamodel.gpsdata.TSPoint;
import com.ibm.spatiotemp.server.datasources.DataSchema;
import com.ibm.spatiotemp.server.datasources.QueryStringFormatter;

public class FakeQueryStringFormatter extends QueryStringFormatter {
	public static Logger logger = Logger.getLogger(FakeQueryStringFormatter.class);
	
	public static String scanquery_format = "select %s,%s,%s from %s where %s = '%s' and %s >= '%s' and %s <= '%s'";
	public static String writedata_format = "insert into %s(%s,%s,%s,%s) values(%s)";
	@Override
	public String RawDataScanQueryString(DataSchema schema, String startts,
			String endts, String movobjid) {
		
		String querystring = String.format(
				scanquery_format, 
				schema.getX_colname(),
				schema.getY_colname(),
				schema.getTs_colname(),
				schema.getTab_name(),
				schema.getMovobj_colname(),
				movobjid,
				schema.getTs_colname(),
				startts,
				schema.getTs_colname(),
				endts
				);
		logger.info(querystring);
		return querystring;
	}

	@Override
	public String writeDataString(DataSchema schema,
			Collection<TSPoint> tspoints) {
		StringBuffer strbuf = new StringBuffer();
		if(tspoints.size()>=1){
			Iterator<TSPoint> iter = tspoints.iterator();
			TSPoint tsPoint = iter.next();
			strbuf.append("("+tsPoint.toString()+")");
			while(iter.hasNext())
			{
				tsPoint = iter.next();
				strbuf.append(",("+tsPoint.toString()+")");
			}
			String querystring = String.format(writedata_format, 
					schema.getTab_name(),
					schema.getMovobj_colname(),
					schema.getX_colname(),
					schema.getY_colname(),
					schema.getTs_colname(),
					strbuf.toString()
					);
			logger.info(querystring);
			return querystring;
		}
		else return "";
	}

}
